﻿Imports System.Drawing
Imports System.Xml

Namespace Othello
    ''' <summary>
    ''' Summary description for ProgramSettings.
    ''' </summary>
    Public Class ProgramSettings
        ' The file name used to save the program settings.
        Private fileName As String

        ' An Xml document used to store and save program settings.
        Private document As XmlDocument

        Public Sub New(ByVal fileName As String)
            '
            ' TODO: Add constructor logic here
            '

            ' Assign the file name.
            Me.fileName = fileName

            ' If the file already exists, load it. Otherwise, create a new document.
            Me.document = New XmlDocument()
            Try
                document.Load(Me.fileName)
            Catch ex As Exception
                ' Create a new XML document and set the root node.
                document = New XmlDocument()
                document.AppendChild(document.CreateElement("ProgramSettings"))
            End Try
        End Sub

        '
        ' Saves the settings XML file.
        '
        Public Sub Save()
            Me.document.Save(Me.fileName)
        End Sub

        '
        ' Reads a value from the settings file.
        '
        Public Function GetValue(ByVal section As String, ByVal name As String) As String
            Try
                Return Me.document.DocumentElement.SelectSingleNode(section & "/" & name).InnerText
            Catch ex As Exception
                Return ""
            End Try
        End Function

        '
        ' Writes a value to the settings file.
        '
        Public Sub SetValue(ByVal section As String, ByVal name As String, ByVal value As String)
            ' If the section does not exist, create it.
            Dim sectionNode As XmlNode = Me.document.DocumentElement.SelectSingleNode(section)
            If sectionNode Is Nothing Then
                sectionNode = Me.document.DocumentElement.AppendChild(Me.document.CreateElement(section))
            End If
            ' If the node does not exist, create it.
            Dim node As XmlNode = sectionNode.SelectSingleNode(name)
            If node Is Nothing Then
                node = sectionNode.AppendChild(Me.document.CreateElement(name))
            End If

            ' Set the value.
            node.InnerText = value
        End Sub
    End Class
End Namespace
